﻿Imports System.Data.SqlClient
Imports Business
Imports System.Data.Common
Imports System.Configuration

Public Class frmLogin

    Dim serverName As String

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        Application.Exit()

    End Sub

    Dim strConnect As String

    Dim sqlConnect As SqlConnection

    ''' <summary>
    ''' login to system
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Login()

        lblMessage.Text = "Working..."

        Application.DoEvents()

        serverName = txtServerName.Text

        Dim builder As New DbConnectionStringBuilder()

        builder.ConnectionString = ConfigurationManager.ConnectionStrings.Item("SAT").ConnectionString

        Dim dataSource As String = builder.Item("Data Source")

        Dim server As String() = dataSource.Split("\")

        builder.Item("Data Source") = serverName & "\" & server(1)

        ' lblMessage.Text = builder.ConnectionString


        If txtUserName.Text <> "" Then

            If txtPassword.Text <> "" Then

                Try

                    Dim bsEmployee As New BSEmployee(builder.ConnectionString)

                    bsEmployee.OpenConnect()

                    bsEmployee.CloseConnect()

                    If bsEmployee.CheckUsernameAndPassword(txtUserName.Text, txtPassword.Text) = 1 Then

                        frmMain.serverName = builder.ConnectionString

                        bsEmployee.CloseConnect()

                        Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

                        Dim conSettings As ConnectionStringsSection = config.GetSection("connectionStrings")

                        conSettings.ConnectionStrings.Remove(ConfigurationManager.ConnectionStrings.Item("SAT"))

                        Dim connectionStringSettings As ConnectionStringSettings = New ConnectionStringSettings("SAT", builder.ConnectionString)

                        conSettings.ConnectionStrings.Add(connectionStringSettings)

                        config.Save(ConfigurationSaveMode.Modified)

                        ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name)

                        Me.Close()

                    Else

                        lblMessage.Text = "User Name and Password is incorrect."

                    End If

                Catch ex As Exception

                    lblMessage.Text = "Server Name can not find!"

                    '  MsgBox(ex.Message)

                End Try

            Else

                lblMessage.Text = "Password can not empty."

            End If

        Else

            lblMessage.Text = "User Name can not empty."

        End If

        Application.DoEvents()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

        Login()

    End Sub


   

    ''' <summary>
    ''' key Enter login
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtPassword_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPassword.KeyDown

        ' check keycode = enter
        If e.KeyCode = Keys.Enter Then

            ' login
            Login()

        End If

    End Sub

    ''' <summary>
    ''' key enter login
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtUserName_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUserName.KeyDown

        ' check keycode = enter
        If e.KeyCode = Keys.Enter Then

            ' login
            Login()

        End If

    End Sub
End Class